package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.common.user.UserCommon;
import com.zhentao.mapper.ChargeUserMapper;
import com.zhentao.mapper.ChargeUserUserProductMapper;
import com.zhentao.market.ChargeMarketThreadUtils;
import com.zhentao.minio.service.impl.MinioFileStorageServiceImpl;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.pojo.user.ChargeUser;
import com.zhentao.model.pojo.user.product.ChargeUserProduct;
import com.zhentao.model.pojo.user.product.ChargeUserUserProduct;
import com.zhentao.service.ChargeUserProductService;
import com.zhentao.mapper.ChargeUserProductMapper;
import com.zhentao.user.ChargeUserThreadUtils;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Wl177
* @description 针对表【charge_user_product】的数据库操作Service实现
* @createDate 2025-01-05 19:11:20
*/
@Service
public class ChargeUserProductServiceImpl extends ServiceImpl<ChargeUserProductMapper, ChargeUserProduct>
    implements ChargeUserProductService{

    @Resource
    private ChargeUserProductMapper productMapper;
    @Resource
    private ChargeUserUserProductMapper userProductMapper;
    @Resource
    private MinioFileStorageServiceImpl minioFileStorageService;
    @Resource
    private ChargeUserMapper chargeUserMapper;


    @Override
    public ResponseResult getProducts() {
        List<ChargeUserProduct> chargeUserProducts = productMapper.selectList(null);
        return ResponseResult.okResult(chargeUserProducts);
    }

    @Override
    public ResponseResult addProduct(ChargeUserProduct product) {
        //判断参数
        if (product == null) {
            return ResponseResult.errorResult(400, "参数错误");
        }
        //判断商品是否存在
        ChargeUserProduct chargeUserProduct = productMapper.selectOne(Wrappers.<ChargeUserProduct>lambdaQuery().eq(ChargeUserProduct::getProductName, product.getProductName()));
        if (chargeUserProduct != null) {
            return ResponseResult.errorResult(400, "商品已存在");
        }
        int insert = productMapper.insert(product);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult updateProduct(ChargeUserProduct product) {
        if (product == null) {
            return ResponseResult.errorResult(400, "参数错误");
        }
        int i = productMapper.updateById(product);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult deleteProduct(Integer id) {
        //判断参数
        if (id == null) {
            return ResponseResult.errorResult(400, "参数错误");
        }
        int i = productMapper.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult exchangeProdut(Integer id) {
        //判断参数
        if (id == null) {
            return ResponseResult.errorResult(400, "参数错误");
        }
        //获取当前登录对象
        ChargeUser chargeUser = ChargeUserThreadUtils.getChargeUserThreadLocal();
        ChargeUser chargeUser1 = chargeUserMapper.selectOne(Wrappers.<ChargeUser>lambdaQuery().eq(ChargeUser::getId, chargeUser.getId()));
        ChargeUserProduct Product = productMapper.selectOne(Wrappers.<ChargeUserProduct>lambdaQuery().eq(ChargeUserProduct::getId, id));
        //判断积分是否充足
        if(chargeUser1.getPoint()<Product.getPoint()){
            return ResponseResult.errorResult(400,"积分不足");
        }
        ChargeUserUserProduct userProduct = new ChargeUserUserProduct();
        userProduct.setUserId(chargeUser.getId().intValue());
        userProduct.setProductId(id);
        userProduct.setCreateTime(new Date());
        int insert = userProductMapper.insert(userProduct);
        return ResponseResult.okResult("兑换成功");
    }

    @Override
    public ResponseResult getUserProducts() {
        ChargeUser chargeUser = ChargeUserThreadUtils.getChargeUserThreadLocal();
        List<ChargeUserUserProduct> chargeUserUserProducts = userProductMapper.selectList(Wrappers.<ChargeUserUserProduct>lambdaQuery().eq(ChargeUserUserProduct::getUserId, chargeUser.getId()));
        for (ChargeUserUserProduct chargeUserUserProduct : chargeUserUserProducts) {
            ChargeUserProduct chargeUserProduct = productMapper.selectOne(Wrappers.<ChargeUserProduct>lambdaQuery().eq(ChargeUserProduct::getId, chargeUserUserProduct.getProductId()));
            chargeUserUserProduct.setChargeUserProduct(chargeUserProduct);
        }
        return ResponseResult.okResult(chargeUserUserProducts);
    }

    @Override
    @SneakyThrows
    public ResponseResult uploadImg(MultipartFile file) {
        if (file != null) {
            return ResponseResult.okResult(file.getOriginalFilename());
        }
        String path = minioFileStorageService.
                uploadImgFile(UserCommon.CHARGE_USER_REDIS_PREFIX_AVATAR_IMG, file.getOriginalFilename(), file.getInputStream());
        return ResponseResult.okResult(path);
    }


}




